use "Colombia LAPOP AmericasBarometer 2018 v1.0_W.dta", replace 
set scheme white_ptol
* ---------------- *
* Data preparation *
* ---------------- *
***************************************
* day variable and treatment variable *
***************************************
tab fecha // date of interview
drop if fecha==td(7oct2018) | fecha==td(28oct2018) //drop day of 1st round and 2nd round election
gen day = fecha - td(10sep2018) 
tab day fecha 

* tr group 
gen group=.
replace group=0 if fecha<td(7oct2018) //before 1st round
replace group=1 if fecha>td(7oct2018) & fecha<td(28oct2018) //btw 1st and 2nd round
replace group=2 if fecha>td(28oct2018) //after 2nd round
label define lbgr 0"Pre-runoff" 1"Runoff" 2"Post-2nd round"
label values group lbgr
tab group, m // 63.14; 4.63; 32.05

********************************
* socio-demographic covariates *
********************************
tab q2, m
rename q2 age

fre sex
recode sex (1=0) (2=1)
label define lbsex 0"Sex: Male" 1"Sex: Female" 
label values sex lbsex
fre sex

tab q10new
tab q10new, m
tab q10new, m nol
rename q10new income

fre ur
recode ur (1=0) (2=1)
label define lbur 0"Place of residence: Urban" 1"Place of residence: Rural" 
label values ur lbur

fre sex

tab ed
tab ed, m
tab ed, m nol

fre colocup4a
clonevar work=colocup4a
label define lbwork 1"Working" 2"Not working at the moment but have a job" 3"Actively looking for work" 4"being student" ///
			5"Dedicated to household chores" 6"Permanently disabled from working" 7"Retired" 8"Does not work and is not looking for work"
label values work lbwork
fre work

fre q3cn
clonevar reli=q3cn
recode reli (77=3) (7=3)
label define lbreli 1"Catholic" 2"Traditional Protestant or Non-Evangelical Protestant" ///
				3"Traditional Religions, Non-Christian Eastern Religions, and other" ///
				4"None (Believe in a Higher Being but do not belongs to no religion)" 5"Evangelical and Pentecostal" ///
				11"Agnostic or atheist (does not believe in God)"  
label values reli lbreli
fre reli

* define control variables
global controls "age c.age#c.age i.sex ed income i.ur i.work i.reli"

******************
* key moderators *
******************
* ideological self-placement
tab l1, m
tab l1, m nol
rename l1 lr_self
label define lblr_self 1"Left" 10"Right"
label values lr_self lblr_self

* recall vote in 2nd round of the presidential election
tab vb2v vb3n2, m
tab vb2v, m
tab vb3n2, m
gen vote_2nd=.
replace vote_2nd=1 if vb3n2==801
replace vote_2nd=2 if vb3n2==802
replace vote_2nd=3 if vb2v==2
replace vote_2nd=3 if vb3n2==0 | vb3n2==97 | vb3n2==899
label define lbvote_2nd 1"Recall vote: Iván Duque" 2"Recall vote: Gustavo Petro" 3"Recall vote: Abstain/blank vote"		
label values vote_2nd lbvote_2nd
fre vote_2nd
tab vote_2nd vb2v
					
*******************
* rename outcomes *
*******************
rename b47a etrust
rename b6 support
rename ing4 demo

***************************
* creating entropy weight *
***************************
gen group_c=. // combining the runoff group and the post-second-round group into one group
replace group_c=0 if group==0
replace group_c=1 if group==1 | group==2
replace group_c=. if group==.
fre group_c
ebalance group_c age i.sex ed income ur i.ur i.work i.reli, tar(2) 

******************************************
* Summary statistics (Table A.2.1-A.2.4) *
******************************************
asdoc sum etrust support demo age sex ed income ur lr_self, replace tzok dec(2) save(sumstat.rtf)
fre work 
fre reli
fre vote_2nd

******************************
* Balance tests (Figure A.1) *
******************************
eststo age: reg group_c age
eststo sex: reg group_c i.sex 
eststo ed: reg group_c ed 
eststo income: reg group_c income 
eststo ur: reg group_c i.ur

forvalues i=1/8{
	eststo work`i': reg group_c `i'.work
}
forvalues i=1/7{
	eststo reli`i': reg group_c `i'.reli
}
eststo reli11: reg group_c 11.reli
eststo vote1: reg group_c 1.vote_2nd
eststo vote2: reg group_c 2.vote_2nd
eststo vote3: reg group_c 3.vote_2nd
eststo lr_self: reg group_c lr_self 

label var age "Age"
label var sex "Sex"
label var ed "Years of Schooling"
label var income "Monthly household income"
label var ur "Urban/Rural status"
label var work "Working status"
label var lr_self "Ideological self-placement"

* ------------------------------------------------------------------------------
coefplot age sex ed income ur work1 work2 work3 work4 work5 work6 work7 work8 ///
	reli1 reli2 reli3 reli4 reli5  reli7 reli11 ///
	lr_self vote1 vote2 vote3, ///
	drop(_cons) xtitle("Estimate") xline(0, lpattern(l)) legend(off) mcolor(black)  ciopts(lcolor(black)) offset(0) ///
	headings(lr_self="{bf:Moderators}" 	1.work="{bf:Working status}" 1.reli="{bf:Religion}", labsize(3)) ///
	mlabel format(%9.2g) mlabposition(1) mlabgap(*1)
graph export "fig A.1.png", as(png) replace 


* ---------------------------------- *
* Main Analysis (DV: election trust) *
* ---------------------------------- *
eststo etrust_full: reg etrust i.group $controls // p<0.01
quietly estadd local bw "Full sample": etrust_full
quietly estadd local ctrl "Yes": etrust_full
test 2.group = 1.group
sum etrust
display 0.721/1.868485
display 0.535/1.868485

margins group
marginsplot, title("(a) Election trust", size(medium) pos(11)) ytitle("Predicted level") ///
xtitle("") xlab(-0.5(1)2.5) plot1opts(lpattern("blank")) plotopts(lcolor(black)) ci1opts(color(black)) ///
addplot(scatter etrust group if group==0, jitter(7) legend(off) color(plr1%10) || ///
scatter etrust group if group==1, jitter(7) legend(off) color(plb1%30) || ///
scatter etrust group if group==2, jitter(7) legend(off) color(plb3%10) ///
xlab(-0.5 " " 2.5 " " 0 "Pre-election" 1 "Runoff" 2 "Post-2nd round" ) ///
ylab(1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "6" 7 "7" ))  ///
saving(etrust_3grp, replace) scheme(538bw)
graph export "etrust_3grps.png", replace

* bandwidth (7 days before and after the first-round)
eststo etrust_runoff7: reg etrust i.group $controls if fecha>td(30sep2018) & fecha<td(14oct2018) & group!=2 // 
quietly estadd local bw "7 days before and after first-round": etrust_runoff7
quietly estadd local ctrl "Yes": etrust_runoff7

* bandwidth (last 7 days)
eststo etrust_last7: reg etrust i.group $controls if (fecha>td(30sep2018) & group==0) | (fecha>td(20dec2018) & group==2) // 
quietly estadd local bw "7 days before first-round and last 7 days of survey": etrust_last7
quietly estadd local ctrl "Yes": etrust_last7
display 0.998/1.868485

* ------------------------------------------------------------------------------
* Table 1 
esttab etrust_full etrust_runoff7 etrust_last7 using "table 1.rtf", b(2) se(2) replace ///
	scalars("bw Sample" "ctrl Controls" "N Observations" "r2 R-squared") 	///
	label wide note onecell nobaselevels  noomitted nonumbers  lines   ///
	keep (1.group 2.group _cons) ///
	coeflabel(1.group "Runoff" 2.group "Post2nd") ///
	mtitles("(1)" "(2)" "(3)")  modelwidth(17 17 17) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are coefficients of OLS regression. Standard errors are shown in parentheses. All models include socio-demographic controls (age, square term of age, gender, education level, income, place of residence (rural/urban), working status, and religion).") ///
	title("Table 1. Spillover effects of Jair Bolsonaro's electoral success (DV: Election trust)") 

* ------------------------------------------------------------------------------
* Figure 2 
gen group_r=.
replace group_r=23 if fecha>td(30sep2018) & group==0 // 7 days before 1st round
replace group_r=30 if fecha<td(14oct2018) & group==1 // 7 days after 1st round
replace group_r=105 if fecha>td(20dec2018) & group==2 // last 7 days of the survey
reg etrust i.group_r $controls //if group!=1
margins group_r
marginsplot, xtitle("") xlab(-0.5(1)2.5) plot1opts(lpattern("blank")) plotopts(lcolor(black)) ci1opts(color(black)) ///
addplot(scatter etrust day if  group==0 , mcolor(plr1%10) jitter(2.5) || ///
	   scatter etrust day if  group==1 , mcolor(plb1%30) jitter(2.5)  || ///
	   scatter etrust day if  group==2 , mcolor(plb3%20) jitter(2.5)  || ///
	   scatteri 0.5 26.7 7.5 26.7 7.5 20 0.5 20, recast(area) color(gs12%50) || ///
	   scatteri 0.5 27.3 7.5 27.3 7.5 34 0.5 34, recast(area) color(gs12%50) || ///
	   scatteri 0.5 101 7.5 101 7.5 108 0.5 108, recast(area) color(gs12%50)  ///
	xlabel(0 "10 Sep 2018" 27 "07 Oct 2018" 48 "28 Oct 2018" 108 "27 Dec 2018") ytitle("Predicted level") xtitle("Date", size(small)) ///
	xline(27 48 , lpattern(solid) lcolor(gs10)) legend(off)  graphregion(margin(r+4)) ///
	ylabel(1(1)7)) title("(b) Election trust over time", size(medium)) ///
	saving(temporal, replace) scheme(538bw)
graph export "temporal.png", replace 

graph combine etrust_3grp.gph temporal.gph, row(2) 
graph export "etrust_combine.png", replace 	

* ---------------------------------------- *
* Additional Analysis (DV: election trust) *
* ---------------------------------------- *
**************************
* Restricting bandwidths *
**************************
eststo etrust_runoff6: reg etrust i.group $controls if fecha>td(1oct2018) & fecha<td(13oct2018) & group!=2 // 6 days
quietly estadd local bw "6 days before and after first-round": etrust_runoff6
quietly estadd local ctrl "Yes": etrust_runoff6
eststo etrust_runoff5: reg etrust i.group $controls if fecha>td(2oct2018) & fecha<td(12oct2018) & group!=2 // 5 days
quietly estadd local bw "5 days before and after first-round": etrust_runoff5
quietly estadd local ctrl "Yes": etrust_runoff5
eststo etrust_runoff4: reg etrust i.group $controls if fecha>td(3oct2018) & fecha<td(11oct2018) & group!=2 // 4 days
quietly estadd local bw "4 days before and after first-round": etrust_runoff4
quietly estadd local ctrl "Yes": etrust_runoff4
eststo etrust_runoff3: reg etrust i.group $controls if fecha>td(4oct2018) & fecha<td(10oct2018) & group!=2 // 3 days (n.s.)
quietly estadd local bw "3 days before and after first-round": etrust_runoff3
quietly estadd local ctrl "Yes": etrust_runoff3

esttab etrust_runoff6 etrust_runoff5 etrust_runoff4 etrust_runoff3  using "table B.1.rtf", b(2) se(2) replace ///
	scalars("bw Sample" "ctrl Controls" "N Observations" "r2 R-squared") 	///
	label wide note onecell nobaselevels  noomitted nonumbers  lines varwidth(11) ///
	keep (1.group _cons) ///
	coeflabel(1.group "Runoff") ///
	mtitles("(1)" "(2)" "(3)" "(4)")  modelwidth(12 12 12 12) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are coefficients of OLS regression. Standard errors are shown in parentheses. All models include socio-demographic controls (age, square term of age, gender, education level, income, place of residence (rural/urban), working status, and religion).") ///
	title("Table B.1. Restricting bandwidths: 3-6 days before and after the first round (DV: Election trust)") 

************************
* heterogenous effects *
************************
* moderated by ideological self-placement
eststo etrust_lr_self: reg etrust i.group##c.lr_self $controls
quietly estadd local bw "Full sample": etrust_lr_self
quietly estadd local ctrl "Yes": etrust_lr_self
margins, dydx(group) at(lr_self=(1(1)10))
mplotoffset, title("Election trust by ideological self-placement", size(medium)) ///
	ytitle("Marginal effect of Bolsonaro’s electoral success") ///
	xtitle(" " "Ideological Self-placement") ///
	xlab(1 "1 Left" 2 "2" 3 "3" 4 "4" 5 "5" 6 "6" 7 "7" 8 "8" 9 "8" 10 "10 Right" ) ///
	yline(0, lwidth(2pt) lcolor(red)) ///
	plot1opts(lpattern("blank")) plotopts(lcolor(black)) ci1opts(color(black)) ///
	legend(pos(6) order(3 "Runoff" 4 "Post-2nd round") r(1)) ///
saving(lr_self, replace) scheme(538bw)
graph export "lr_self.png", replace


* moderated by recall vote (2nd round)
eststo etrust_vote_2nd: reg etrust i.group##i.vote_2nd $controls
quietly estadd local bw "Full sample": etrust_vote_2nd
quietly estadd local ctrl "Yes": etrust_vote_2nd
test 1.group=1.group+1.group#2.vote_2nd
test 1.group=1.group+1.group#3.vote_2nd
test 2.group=2.group+2.group#2.vote_2nd
test 2.group=2.group+2.group#3.vote_2nd
margins vote_2nd, dydx(group) 
mplotoffset, title("Election trust by recall vote", size(medium)) ///
	ytitle("Marginal effect of Bolsonaro’s electoral success") ///
	xtitle(" " "Vote choice in the previous presidential election") ///
	xlab(0.5 " " 3.5 " " 1 "Iván Duque" 2 "Gustavo Petro" 3 "Abstain/blank vote") ///
	yline(0, lwidth(2pt) lcolor(red)) ///
	plot1opts(lpattern("blank")) plot2opts(lpattern("blank")) plotopts(lcolor(black)) ci1opts(color(black)) ///
	legend(pos(6) order(3 "Runoff" 4 "Post-2nd round") r(1)) ///
saving(vote_2nd, replace) scheme(538bw)
graph export "vote_2nd.png", replace


esttab etrust_lr_self etrust_vote_2nd  using "table B.2.rtf", b(2) se(2) replace ///
	scalars("bw Sample" "ctrl Controls" "N Observations" "r2 R-squared") 	///
	label wide note onecell nobaselevels  noomitted nonumbers  lines varwidth(32) ///
	keep (1.group 2.group 1.group#c.lr_self 2.group#c.lr_self 2.vote_2nd 3.vote_2nd 1.group#2.vote_2nd 1.group#3.vote_2nd 2.group#2.vote_2nd 2.group#3.vote_2nd _cons) ///
	coeflabel(1.group "Runoff") ///
	mtitles("(1)" "(2)" "(3)" "(4)")  modelwidth(12 12 12 12) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are coefficients of OLS regression. Standard errors are shown in parentheses. All models include socio-demographic controls (age, square term of age, gender, education level, income, place of residence (rural/urban), working status, and religion).") ///
	title("Table B.2. Moderating effects of ideological self-placement and recall vote (DV: Election trust)") 

**********
* ologit *
**********
* full sample
eststo o_etrust_full: ologit etrust i.group $controls 
quietly estadd local bw "Full sample": o_etrust_full
quietly estadd local ctrl "Yes": o_etrust_full
test 2.group = 1.group

* bandwidth (7 days before and after the first-round)
eststo o_etrust_runoff: ologit etrust i.group $controls if fecha>td(30sep2018) & fecha<td(14oct2018) & group!=2 // 
quietly estadd local bw "7 days before and after first-round": o_etrust_runoff
quietly estadd local ctrl "Yes": o_etrust_runoff

* bandwidth (last 7 days)
eststo o_etrust_last7: ologit etrust i.group $controls if (fecha>td(30sep2018) & group==0) | (fecha>td(20dec2018) & group==2) // 
quietly estadd local bw "7 days before first-round and last 7 days of survey": o_etrust_last7
quietly estadd local ctrl "Yes": o_etrust_last7	
	
* ------------------------------------------------------------------------------
* Table 
esttab o_etrust_full o_etrust_runoff o_etrust_last7 using "ologit_etrust.rtf", b(2) se(2) replace ///
	scalars("bw Bandwidth" "ctrl Controls" "N Observations" "r2_p pseudo R-squared") ///
	label wide note onecell nobaselevels  noomitted nonumbers  lines  ///
	keep (1.group 2.group) ///
	coeflabel(1.group "Runoff" 2.group "Post2nd") ///
	mtitles("(1)" "(2)" "(3)")  modelwidth(17 17 17) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are coefficients of ordered logit regression. Standard errors are shown in parentheses. All models include socio-demographic controls (age, square term of age, gender, education level, income, place of residence (rural/urban), working status, and religion).") ///
	title("Table B.3. Ordered logit estimates: Spillover effects of Jair Bolsonaro's electoral success (DV: Election trust)") 

*********************
* entropy balancing *
*********************
* full sample
eststo ebalance_etrust_full: reg etrust i.group [pweight= _webal]
quietly estadd local bw "Full sample": ebalance_etrust_full
quietly estadd local ctrl "Yes": ebalance_etrust_full
test 2.group = 1.group

* bandwidth (runoff)
eststo ebalance_etrust_runoff: reg etrust i.group  if fecha>td(30sep2018) & fecha<td(14oct2018) & group!=2 [pweight= _webal] 
quietly estadd local bw "7 days before and after first-round": ebalance_etrust_runoff
quietly estadd local ctrl "Yes": ebalance_etrust_runoff

* bandwidth (last 7 days)
eststo ebalance_etrust_last7: reg etrust i.group if (fecha>td(30sep2018) & group==0) | (fecha>td(20dec2018) & group==2) [pweight= _webal] 
quietly estadd local bw "7 days before first-round and last 7 days of survey": ebalance_etrust_last7
quietly estadd local ctrl "Yes": ebalance_etrust_last7

* ------------------------------------------------------------------------------
* Table 
esttab ebalance_etrust_full ebalance_etrust_runoff ebalance_etrust_last7 using "ebalance_etrust.rtf", b(2) se(2) replace ///
	scalars("bw Sample" "ctrl Controls" "N Observations" "r2 R-squared") 	///
	label wide note onecell nobaselevels  noomitted nonumbers  lines   ///
	keep (1.group 2.group _cons) ///
	coeflabel(1.group "Runoff" 2.group "Post2nd") ///
	mtitles("(1)" "(2)" "(3)")  modelwidth(17 17 17) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are coefficients of OLS regression. Standard errors are shown in parentheses. Because entropy balancing only allows the treatment indicator to be binary, I combine the runoff group and post-second-round group when creating the entropy weight. Entropy weight is created using socio-demographic controls (age, gender, education level, income, place of residence (rural/urban), working status, and religion). The highest order of moment constraints is set at 2 for each covariate.") ///
	title("Table B.4. Entropy balancing: Spillover effects of Jair Bolsonaro's electoral success (DV: Election trust)") 

***************
* Missingness *
***************
fre etrust
gen m_etrust=.
replace m_etrust=1 if etrust==.a | etrust==.b //  .a No sabe (I don't know') .b No responde 
replace m_etrust=0 if etrust!=. & etrust!=.a & etrust!=.b

eststo m_etrust_full: reg m_etrust i.group $controls 
quietly estadd local bw "Full sample": m_etrust_full
quietly estadd local ctrl "Yes": m_etrust_full
test 2.group = 1.group 

* ------------------------------------------------------------------------------
* Table 
esttab m_etrust_full using "m_etrust.rtf", b(2) se(2) replace ///
	scalars("bw Sample" "ctrl Controls" "N Observations" "r2 R-squared") 	///
	label wide note onecell nobaselevels  noomitted nonumbers  lines   ///
	keep (1.group 2.group _cons) ///
	coeflabel(1.group "Runoff" 2.group "Post2nd") ///
	mtitles("(1)")  modelwidth(17 17 17) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note:  Entries are coefficients of OLS regression. Standard errors are shown in parentheses. The dependent variable is a dummy coded as 1 if respondents answered 'I don't know' or did not provide a response. The model includes socio-demographic controls (age, square term of age, gender, education level, income, place of residence (rural/urban), working status, and religion).") ///
	title("Table B.5. Missingness of election trust") 
	
* ------------------------ *
* Regression for mediators *
* ------------------------ *
*****************************************
* Support for domestic political system *
*****************************************
eststo support_full: reg support i.group $controls 
quietly estadd local bw "Full sample": support_full
quietly estadd local ctrl "Yes": support_full
test 2.group = 1.group
sum support
display 0.456/1.900913
margins group
marginsplot, title("Support the political system of Colombia", size(medium))  ytitle("Predicted level") ///
xtitle("") xlab(-0.5(1)2.5) plot1opts(lpattern("blank")) plotopts(lcolor(black)) ci1opts(color(black)) ///
addplot(scatter support group if group==0, jitter(7) legend(off) color(plr1%10) || ///
scatter support group if group==1, jitter(7) legend(off) color(plb1%30) || ///
scatter support group if group==2, jitter(7) legend(off) color(plb3%10) ///
xlab(-0.5 " " 2.5 " " 0 "Pre-election" 1 "Runoff" 2 "Post-2nd round" ) ///
ylab(1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "6" 7 "7")) ///
saving(support, replace) scheme(538bw)

************************************
* Principled support for democracy *
************************************
eststo demo_full: reg demo i.group $controls 
quietly estadd local bw "Full sample": demo_full
quietly estadd local ctrl "Yes": demo_full
test 2.group = 1.group
sum demo
display 0.314/1.657116
margins group
marginsplot, title("Democracy is better than other forms of government", size(medium))  ytitle("Predicted level") ///
xtitle("") xlab(-0.5(1)2.5) plot1opts(lpattern("blank")) plotopts(lcolor(black)) ci1opts(color(black)) ///
addplot(scatter etrust group if group==0, jitter(7) legend(off) color(plr1%10) || ///
scatter demo group if group==1, jitter(7) legend(off) color(plb1%30) || ///
scatter demo group if group==2, jitter(7) legend(off) color(plb3%10) ///
xlab(-0.5 " " 2.5 " " 0 "Pre-election" 1 "Runoff" 2 "Post-2nd round" ) ///
ylab(1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "6" 7 "7" )) /// 
saving(demo, replace) scheme(538bw)
	
graph combine support.gph demo.gph
graph export "support+demo.png", replace

* ------------------------------------------------------------------------------
* Table 
esttab support_full demo_full  using "support+demo.rtf", b(2) se(2) replace ///
	scalars("bw Sample" "ctrl Controls" "N Observations" "r2 R-squared") 	///
	label wide note onecell nobaselevels  noomitted nonumbers  lines   ///
	keep (1.group 2.group _cons) ///
	coeflabel(1.group "Runoff" 2.group "Post2nd") ///
	mtitles("(1) Support for the political system" "(2) Principled support for democracy" "(3)")  modelwidth(17 17 17) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are coefficients of OLS regression. Standard errors are shown in parentheses. All models include socio-demographic controls (age, square term of age, gender, education level, income, place of residence (rural/urban), working status, and religion).") ///
	title("Table C.1. Spillover effects of Jair Bolsonaro's electoral success (DV: Support for the political system and principled support for democracy)") 


* ------------------------------- *
* Robustness checks for mediators *
* ------------------------------- *
**********
* ologit *
**********
*** Support for the political system
eststo o_support_full: ologit support i.group $controls
test 2.group = 1.group	
quietly estadd local bw "Full sample": o_support_full
quietly estadd local ctrl "Yes": o_support_full

*** Principled support for democracy
eststo o_demo_full: ologit demo i.group $controls
test 2.group = 1.group	
quietly estadd local bw "Full sample": o_demo_full
quietly estadd local ctrl "Yes": o_demo_full
	
* ------------------------------------------------------------------------------
* Table 
esttab o_support_full o_demo_full using "ologit_support+demo.rtf", b(2) se(2) replace ///
	scalars("bw Bandwidth" "ctrl Controls" "N Observations" "r2_p pseudo R-squared") ///
	label wide note onecell nobaselevels  noomitted nonumbers  lines  ///
	keep (1.group 2.group) ///
	coeflabel(1.group "Runoff" 2.group "Post2nd") ///
	mtitles("(1) Support for the political system" "(2) Principled support for democracy")  modelwidth(17 17 17) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are coefficients of ordered logit regression. Standard errors are shown in parentheses. All models include socio-demographic controls (age, square term of age, gender, education level, income, place of residence (rural/urban), working status, and religion).") ///
	title("Table C.2. Ordered logit estimates: Spillover effects of Jair Bolsonaro's electoral success (DV: Support for the political system and principled support for democracy)") 


*********************
* Entropy balancing *
*********************
*** Support for the political system
eststo ebalance_support_full: reg support i.group [pweight= _webal]
quietly estadd local bw "Full sample": ebalance_support_full
quietly estadd local ctrl "Yes": ebalance_support_full
test 2.group = 1.group	

*** Principled support for democracy	
eststo ebalance_demo_full: reg demo i.group [pweight= _webal]
quietly estadd local bw "Full sample": ebalance_demo_full
quietly estadd local ctrl "Yes": ebalance_demo_full
test 2.group = 1.group

* ------------------------------------------------------------------------------
* Table 
esttab ebalance_support_full ebalance_demo_full using "ebalance_support+demo.rtf", b(2) se(2) replace ///
	scalars("bw Sample" "ctrl Controls" "N Observations" "r2 R-squared") 	///
	label wide note onecell nobaselevels  noomitted nonumbers  lines   ///
	keep (1.group 2.group _cons) ///
	coeflabel(1.group "Runoff" 2.group "Post2nd") ///
	mtitles("(1) Support for the political system" "(2) Principled support for democracy")  modelwidth(17 17 17) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are coefficients of OLS regression. Standard errors are shown in parentheses. Because entropy balancing only allows the treatment indicator to be binary, I combine the runoff group and post-second-round group when creating the entropy weight. Entropy weight is created using socio-demographic controls (age, gender, education level, income, place of residence (rural/urban), working status, and religion). The highest order of moment constraints is set at 2 for each covariate.") ///
	title("Table C.3. Entropy balancing: Spillover effects of Jair Bolsonaro's electoral success (DV: Support for the political system and principled support for democracy)") 	
	
***************
* Missingness *
***************
*** Support for the political system	
fre support
gen m_support=.
replace m_support=1 if support==.a | support==.b //  .a No sabe (I don't know') .b No responde 
replace m_support=0 if support!=. & support!=.a & support!=.b

eststo m_support_full: reg m_support i.group $controls 
quietly estadd local bw "Full sample": m_support_full
quietly estadd local ctrl "Yes": m_support_full
test 2.group = 1.group 


*** Principled support for democracy	
fre demo
gen m_demo=.
replace m_demo=1 if demo==.a | demo==.b //  .a No sabe (I don't know') .b No responde 
replace m_demo=0 if demo!=. & demo!=.a & demo!=.b

eststo m_demo_full: reg m_demo i.group $controls 
quietly estadd local bw "Full sample": m_demo_full
quietly estadd local ctrl "Yes": m_demo_full
test 2.group = 1.group 

	
* ------------------------------------------------------------------------------
* Table 
esttab m_support_full m_demo_full using "m_support+demo.rtf", b(2) se(2) replace ///
	scalars("bw Sample" "ctrl Controls" "N Observations" "r2 R-squared") 	///
	label wide note onecell nobaselevels  noomitted nonumbers  lines   ///varwidth(15)
	keep (1.group 2.group _cons) ///
	coeflabel(1.group "Runoff" 2.group "Post2nd") ///
	mtitles("(1) Support for the political system" "(2) Principled support for democracy") modelwidth(17 17 17) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are coefficients of OLS regression. Standard errors are shown in parentheses. The dependent variable is a dummy coded as 1 if respondents answered 'I don't know' or did not provide a response. All models include socio-demographic controls (age, square term of age, gender, education level, income, place of residence (rural/urban), working status, and religion).") ///
	title("Table C.4. Missingness of support for the political system and principled support for democracy") 

	
* ------------------ *
* mediation analysis *
* ------------------ *
*** Support for the political system	
gsem (etrust<-support group_c $controls) ///
	(support<-group_c $controls), nocaps
nlcom indirect:_b[support:group_c]*_b[etrust:support]
display .1671639/(.1671639+.3908236)*100 //Proportion mediated

*** Principled support for democracy		
gsem (etrust<-demo group_c $controls) ///
	(demo<-group_c $controls), nocaps
nlcom indirect:_b[demo:group_c]*_b[etrust:demo]
display .0745678/(.0745678+.4708622)*100 // Proportion mediated

	
* ------------------------------ *
* Placebo using the 2016 dataset *
* ------------------------------ *
use "Colombia LAPOP AmericasBarometer 2016.dta", replace 	
* ----------------------------------------------------------------------------
* Data preparation
* ----------------------------------------------------------------------------
***************************************
* day variable and treatment variable *
***************************************
tab fecha // date of interview
drop if fecha==td(24aug2016) | fecha==td(20sep2016) //drop day of 1st round and 2nd round election (pseudo)
gen day_p = fecha - td(03aug2016) 
tab day_p fecha 	
	
* pseudo-tr group 
gen group_p=.
replace group_p=0 if fecha<td(24aug2016)
replace group_p=1 if fecha>td(24aug2016) & fecha<td(20sep2016) 
replace group_p=2 if fecha>td(20sep2016)
label define lbgr_p 0"Pre-runoff" 1"Runoff" 2"Post-2nd round"
label values group_p lbgr_p
tab group_p, m 
	
********************************
* socio-demographic covariates *
********************************
tab q2, m
rename q2 age

tab sex, m

tab q10new
tab q10new, m
tab q10new, m nol
rename q10new income

tab ur, m

tab ed
tab ed, m
tab ed, m nol

fre ocup4a
rename ocup4a work 

fre q3c
clonevar reli=q3c
recode reli (77=3) (7=3) (6=3) (12=3)
label define lbreli_p 1"Catholic" 2"Traditional Protestant or Non-Evangelical Protestant" ///
				3"Traditional Religions, Non-Christian Eastern Religions, Mormon, Jehovah's Witness and other" ///
				4"None (Believe in a Higher Being but do not belongs to no religion)" 5"Evangelical and Pentecostal" ///
				11"Agnostic or atheist (does not believe in God)"  
label values reli lbreli_p
fre reli

* define control variables
global controls "age c.age#c.age i.sex ed income i.ur i.work i.reli"
	
*******************
* rename outcomes *
*******************
rename b47a etrust
tab etrust, m	
tab etrust, m nol

rename b6 support
tab support, m	
tab support, m nol

rename ing4 demo
tab demo, m	
tab demo, m nol

*** election trust	
eststo etrust_full_p: reg etrust i.group_p $controls 
quietly estadd local bw "Full sample": etrust_full_p
quietly estadd local ctrl "Yes": etrust_full_p
test 2.group_p = 1.group_p

margins group_p
marginsplot, title("Election trust", size(medium) pos(11)) ytitle("Predicted level") ///
xtitle("") xlab(-0.5(1)2.5) plot1opts(lpattern("blank")) plotopts(lcolor(black)) ci1opts(color(black)) ///
addplot(scatter etrust group_p if group_p==0, jitter(7) legend(off) color(plr1%10) || ///
scatter etrust group_p if group_p==1, jitter(7) legend(off) color(plb1%10) || ///
scatter etrust group_p if group_p==2, jitter(7) legend(off) color(plb3%10) ///
xlab(-0.5 " " 2.5 " " 0 "Pre-election" 1 "Runoff" 2 "Post-2nd round" ) ///
ylab(1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "6" 7 "7" ))  ///
saving(etrust_p, replace) scheme(538bw)
graph export "placebo_etrust.png", replace	

* ------------------------------------------------------------------------------
* Table 
esttab etrust_full_p using "table placebo_etrust.rtf", b(2) se(2) replace ///
	scalars("bw Sample" "ctrl Controls" "N Observations" "r2 R-squared") 	///
	label wide note onecell nobaselevels  noomitted nonumbers  lines   ///
	keep (1.group_p 2.group_p _cons) ///
	coeflabel(1.group_p "Runoff" 2.group_p "Post2nd") ///
	mtitles("(1) Election trust" "(2) Support for the political system" "(3) Principled support for democracy")  modelwidth(17 17 17) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are coefficients of OLS regression. Standard errors are shown in parentheses. All models include socio-demographic controls (age, square term of age, gender, education level, income, place of residence (rural/urban), working status, and religion). Data: 2016 AmericasBarometer in Colombia.") ///
	title("Table B.6. Placebo tests using the 2016 AmericasBarometer in Colombia (DV: Election trust)") 

	
*** Support for the political system	
eststo support_full_p: reg support i.group_p $controls 
quietly estadd local bw "Full sample": support_full_p
quietly estadd local ctrl "Yes": support_full_p
test 2.group_p = 1.group_p
margins group_p
marginsplot, title("Support the political system of Colombia", size(medium))  ytitle("Predicted level") ///
xtitle("") xlab(-0.5(1)2.5) plot1opts(lpattern("blank")) plotopts(lcolor(black)) ci1opts(color(black)) ///
addplot(scatter support group_p if group_p==0, jitter(7) legend(off) color(plr1%10) || ///
scatter support group_p if group_p==1, jitter(7) legend(off) color(plb1%10) || ///
scatter support group_p if group_p==2, jitter(7) legend(off) color(plb3%10) ///
xlab(-0.5 " " 2.5 " " 0 "Pre-election" 1 "Runoff" 2 "Post-2nd round" ) ///
ylab(1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "6" 7 "7")) ///
saving(support_p, replace) scheme(538bw)
graph export "placebo_support.png", replace	

*** Principled support for democracy	
eststo demo_full_p: reg demo i.group_p $controls // group 2 is more likely to disagree that democracy is better than any other form of government (p<0.01)
quietly estadd local bw "Full sample": demo_full_p
quietly estadd local ctrl "Yes": demo_full_p
test 2.group_p = 1.group_p
margins group_p
marginsplot, title("Democracy is better than other forms of government", size(medium))  ytitle("Predicted level") ///
xtitle("") xlab(-0.5(1)2.5) plot1opts(lpattern("blank")) plotopts(lcolor(black)) ci1opts(color(black)) ///
addplot(scatter demo group_p if group_p==0, jitter(7) legend(off) color(plr1%10) || ///
scatter demo group_p if group_p==1, jitter(7) legend(off) color(plb1%10) || ///
scatter demo group_p if group_p==2, jitter(7) legend(off) color(plb3%10) ///
xlab(-0.5 " " 2.5 " " 0 "Pre-election" 1 "Runoff" 2 "Post-2nd round" ) ///
ylab(1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "6" 7 "7" )) /// 
saving(demo_p, replace) scheme(538bw)
graph export "placebo_demo.png", replace	

graph combine support_p.gph demo_p.gph, row(1) 
graph export "placebo_support+demo.png", replace 	

* ------------------------------------------------------------------------------
* Table
esttab  support_full_p demo_full_p using "table placebo_support+demo.rtf", b(2) se(2) replace ///
	scalars("bw Sample" "ctrl Controls" "N Observations" "r2 R-squared") 	///
	label wide note onecell nobaselevels  noomitted nonumbers  lines   ///
	keep (1.group_p 2.group_p _cons) ///
	coeflabel(1.group_p "Runoff" 2.group_p "Post2nd") ///
	mtitles("(1) Support for the political system" "(2) Principled support for democracy")  modelwidth(17 17 17) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are coefficients of OLS regression. Standard errors are shown in parentheses. All models include socio-demographic controls (age, square term of age, gender, education level, income, place of residence (rural/urban), working status, and religion). Data: 2016 AmericasBarometer in Colombia.") ///
	title("Table C.5. Placebo tests using the 2016 AmericasBarometer in Colombia (DV: Support for the political system and principled support for democracy)") 
	
